<template>
  <div class="dv-border-box-3" ref="borderBox3">
    <svg class="dv-border-svg-container" :width="width" :height="height">
      <polygon :fill="backgroundColor" :points="`
        23, 23 ${width - 24}, 23 ${width - 24}, ${height - 24} 23, ${height - 24}
      `"/>

      <polyline class="dv-bb3-line1"
                :stroke="mergedColor[0]"
                :points="`4, 4 ${width - 22} ,4 ${width - 22}, ${height - 22} 4, ${height - 22} 4, 4`"
      />
      <polyline class="dv-bb3-line2"
                :stroke="mergedColor[1]"
                :points="`10, 10 ${width - 16}, 10 ${width - 16}, ${height - 16} 10, ${height - 16} 10, 10`"
      />
      <polyline class="dv-bb3-line2"
                :stroke="mergedColor[1]"
                :points="`16, 16 ${width - 10}, 16 ${width - 10}, ${height - 10} 16, ${height - 10} 16, 16`"
      />
      <polyline class="dv-bb3-line2"
                :stroke="mergedColor[1]"
                :points="`22, 22 ${width - 4}, 22 ${width - 4}, ${height - 4} 22, ${height - 4} 22, 22`"
      />
    </svg>

    <div class="border-box-content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import { cloneDeep, debounce, mergeDeep } from "../../../../utils/utils";
import { defineComponent, onMounted, ref, watch } from "vue";

export default defineComponent({
  name: "DvBorderBox3",
  props: {
    color: {
      type: Array,
      default: () => ([])
    },
    backgroundColor: {
      type: String,
      default: "transparent"
    }
  },
  setup(props) {
    const borderBox3 = ref();
    const defaultColor = ref(["#2862b7", "#2862b7"]);
    const mergedColor = ref([]);
    const width = ref(300);
    const height = ref(300);
    const mergeColor = () => {
      mergedColor.value = mergeDeep(cloneDeep(defaultColor.value), props.color || []);
    };
    watch(() => props.color, s => {
      mergeColor();
    });
    onMounted(() => {
      const ro = new ResizeObserver(entries => {
        handleResize(entries);
      });
      ro.observe(borderBox3.value);
      mergeColor();
    });
    const handleResize = debounce((entries) => {
      width.value = entries[0].contentRect.width;
      height.value = entries[0].contentRect.height;
    }, 100);
    return { width, height, borderBox3, mergedColor, mergeColor, handleResize, defaultColor, };
  },
});
</script>

<style scoped>
.dv-border-box-3 {
  position: relative;
  width: 100%;
  height: 100%;
}

.dv-border-box-3 .dv-border-svg-container {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0px;
  left: 0px;
}

.dv-border-box-3 .dv-border-svg-container > polyline {
  fill: none;
}

.dv-border-box-3 .dv-bb3-line1 {
  stroke-width: 3;
}

.dv-border-box-3 .dv-bb3-line2 {
  stroke-width: 1;
}

.dv-border-box-3 .border-box-content {
  position: relative;
  width: 100%;
  height: 100%;
}
</style>
